package com.service.impl;

import com.dao.CommentInfoVoRepository;
import com.pojo.GroupOrder;
import com.pojo.User;
import com.pojoVO.CommentInfoVo;
import com.pojoVO.GroupComStarVO;
import com.service.CommentInfoVoService;
import com.service.GroupOrderService;
import com.service.UserService;
import com.util.ServerResponse;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/*
 * Created by Administrator on 2020/4/14 0014.
 */

@Service
public class CommentInfoVoServiceImpl implements CommentInfoVoService {
    @Autowired
    private AmqpTemplate amqpTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    GroupOrderService groupOrderService;
    @Autowired
    private CommentInfoVoRepository commentInfoVoRepository;
    @Autowired
    UserService userService;
    @Override
    public ServerResponse save(CommentInfoVo commentInfoVo) {
        Byte status =1;
        UUID uuid = UUID.randomUUID();
        //  commentInfoVo.setReplyInfos(replyInfos);
        String commentId=uuid.toString();
        commentId=commentId.replace("-","");
        commentInfoVo.setCommentId(commentId);
        commentInfoVo.setCreatetime(new Date());
        commentInfoVo.setStatus(status);
        commentInfoVo.setParentId("1");
        commentInfoVo.setReplynum(0);
        //执行mybatis添加方法
        CommentInfoVo save = commentInfoVoRepository.save(commentInfoVo);
        //判断添加成功
        if (save!=null)
            return ServerResponse.addSuccess();
        else
            return ServerResponse.addFailed();
    }
    @Override
    public ServerResponse modify(CommentInfoVo commentInfoVo) {
        String commentId = commentInfoVo.getCommentId();
        CommentInfoVo commentInfo = commentInfoVoRepository.findById(commentId).get();
        CommentInfoVo commentInfoVo1 = mergeCommentInfoVo(commentInfo, commentInfoVo);
        CommentInfoVo save = commentInfoVoRepository.save(commentInfoVo1);
        if (save!=null)
            return ServerResponse.updateSuccess();
        return ServerResponse.updateFailed();
    }
    @Override
    public ServerResponse modifyStatus(String id) {
        CommentInfoVo commentInfoVo = commentInfoVoRepository.findById(id).get();
        commentInfoVo.setStatus(Byte.valueOf("0"));
        CommentInfoVo save = commentInfoVoRepository.save(commentInfoVo);
        if (save!=null)
            return ServerResponse.deleteSuccess();
        return ServerResponse.deleteFailed();
    }



    public ServerResponse getById(String id) {
        CommentInfoVo commentInfoVo = commentInfoVoRepository.findById(id).get();
        if (commentInfoVo!=null)
            return ServerResponse.getDataSuccess(commentInfoVo);
        return ServerResponse.getDataFailed("没有该评论");
    }

    @Override
    public ServerResponse getAll() {
        List<CommentInfoVo> list = commentInfoVoRepository.findAll();
        if (list!=null&&list.size()>0)
            return ServerResponse.getDataSuccess(list);
        return ServerResponse.getDataFailed("没有数据");
    }

    @Override
    public ServerResponse getByGroupId(String groupId) {

        List<CommentInfoVo> list= commentInfoVoRepository.findByGroupIdAndStatus(Integer.valueOf(groupId),Byte.valueOf("1"));
        if (list!=null&&list.size()>0)
            return ServerResponse.getDataSuccess(list);
        return ServerResponse.getDataFailed("没有数据");
    }

    @Override
    public ServerResponse getByUserId(Integer userId) {
        List<CommentInfoVo> list= commentInfoVoRepository.findByUserIdAndStatusAndParentId(Integer.valueOf(userId),Byte.valueOf("1"),"1");
        if (list!=null&&list.size()>0)
            return ServerResponse.getDataSuccess(list);
        return ServerResponse.getDataFailed("没有数据");
    }

    @Override
    public ServerResponse getstarNumByGroupId(String groupId) {
        if (this.getByGroupId(groupId).getStatus()==0)
            return this.getByGroupId(groupId);
        List<CommentInfoVo> list= (ArrayList<CommentInfoVo>)this.getByGroupId(groupId).getData();
        Integer oneStarNum=0;
        Integer twoStarNum=0;
        Integer threeStarNum=0;
        Integer fourStarNum=0;
        Integer fiveStarNum=0;
        Integer allStarNum=0;
        Integer StarTol=0;
        Float avaStar=0f;


        if (list!=null&&list.size()>0) {
            for (CommentInfoVo g : list) {
                Integer star = Integer.valueOf(g.getStar());
                if (star != null) {
                    allStarNum++;
                    StarTol += star;
                    switch (star) {
                        case 2:twoStarNum++;break;
                        case 3:threeStarNum++;break;
                        case 4:fourStarNum++;break;
                        case 5:fiveStarNum++;break;
                        case 1:oneStarNum++;break;
                    }
                }
            }
            DecimalFormat df=new DecimalFormat("0.0");
            avaStar=(float)StarTol/allStarNum;
            String format = df.format(avaStar);
            // String format=avaStar.toString().substring(4);
            System.out.println("avaStar=="+avaStar);
            System.out.println("format=="+format);
            GroupComStarVO groupComStarVO = new GroupComStarVO(oneStarNum,twoStarNum,threeStarNum,
                    fourStarNum,fiveStarNum,allStarNum,Float.valueOf(format));
            return ServerResponse.getDataSuccess(groupComStarVO);
        }
        return ServerResponse.getDataFailed("没有数据");
    }

    @Override
    public ServerResponse getByOrderNum(Long orderNum) {
        CommentInfoVo commentInfoVo= commentInfoVoRepository.findByOrderNumAndStatus(orderNum,Byte.valueOf("1"));
        if (commentInfoVo!=null){
            return ServerResponse.getDataSuccess(commentInfoVo);
        }
        return ServerResponse.getDataFailed("没有数据");
    }
    @Override
    public ServerResponse findByParentId(String parentId, Integer page) {
        Page<CommentInfoVo> commentInfoVoPage = commentInfoVoRepository.findByParentId(parentId, PageRequest.of(page-1, 5));
        List<CommentInfoVo> content = commentInfoVoPage.getContent();
        if (content!=null){
            return ServerResponse.getDataSuccess(content);
        }
        return ServerResponse.getDataFailed("没有数据");
    }

    @Override
    public ServerResponse getByReplyUserId(Integer userId) {
        ArrayList<CommentInfoVo> byReplyUserId = commentInfoVoRepository.findByReplyUserId(userId);
        if (byReplyUserId!=null){
            return ServerResponse.getDataSuccess(byReplyUserId);
        }
        return ServerResponse.getDataFailed("没有数据");
    }

    @Override
    public ServerResponse submitCommet(CommentInfoVo commentInfoVo) {
        Long orderNum = commentInfoVo.getOrderNum();
        ServerResponse groupOrderst = groupOrderService.getByOrderNum(orderNum);
        if (groupOrderst.getStatus()!=1)
            return groupOrderst;
        GroupOrder groupOrder =(GroupOrder)groupOrderst.getData();
        Integer userId = groupOrder.getUserId();
        Integer groupId = groupOrder.getGroupId();
        ServerResponse usersr = userService.getById(userId);
        User user =(User) usersr.getData();
        String userName = user.getUserName();
        commentInfoVo.setUserId(userId);
        commentInfoVo.setGroupId(groupId);
        commentInfoVo.setUserName(userName);
        return this.save(commentInfoVo);
    }


    @Override

    public ServerResponse saveReply(CommentInfoVo commentInfoVo) {
        Byte status =1;
        UUID uuid = UUID.randomUUID();
        //  commentInfoVo.setReplyInfos(replyInfos);
        String commentId=uuid.toString();
        commentId=commentId.replace("-","");
        commentInfoVo.setCommentId(commentId);
        commentInfoVo.setCreatetime(new Date());
        commentInfoVo.setStatus(status);
        if (commentInfoVo.getParentId()==null)
            return ServerResponse.addFailed();
        if (commentInfoVo.getReplyUserId()==null)
            return ServerResponse.addFailed();
        ServerResponse usersr = userService.getById(commentInfoVo.getUserId());
        User user =(User) usersr.getData();
        if (user.getUserName()!=null){
            commentInfoVo.setUserName(user.getUserName());
        }else {
            commentInfoVo.setUserName("匿名用户");
        }
        ServerResponse userReplysr = userService.getById(commentInfoVo.getReplyUserId());
        User userReply =(User) usersr.getData();
        if (user.getUserName()!=null){
            commentInfoVo.setReplyUserName(user.getUserName());
        }else {
            commentInfoVo.setReplyUserName("匿名用户");
        }
        CommentInfoVo ParentcommentInfo=new CommentInfoVo();
        ParentcommentInfo.setCommentId(commentInfoVo.getParentId());
        ParentcommentInfo.setReplynum(-1);
        this.modify(ParentcommentInfo);
        rabbitTemplate.convertAndSend("topicExchange","systemInfo",commentInfoVo);
        CommentInfoVo save = commentInfoVoRepository.save(commentInfoVo);
        if (save!=null)
            return ServerResponse.addSuccess();
        return ServerResponse.addFailed();
    }
/*
     *
     * @param findByMg mongodb查询到的信息
     * @param modifyInfo 修改的信息
     * @return 修改的信息的json形式
     */

    CommentInfoVo mergeCommentInfoVo(CommentInfoVo findByMg,CommentInfoVo modifyInfo){
        if (modifyInfo.getUserId()==null&&findByMg.getUserId()!=null){
            modifyInfo.setUserId(findByMg.getUserId());
        }
        if (modifyInfo.getGroupId()==null&&findByMg.getGroupId()!=null){
            modifyInfo.setGroupId(findByMg.getGroupId());
        }
        if (modifyInfo.getStar()==null&&findByMg.getStar()!=null){
            modifyInfo.setStar(findByMg.getStar());
        }
        if (modifyInfo.getCommentContext()==null&&findByMg.getCommentContext()!=null){
            modifyInfo.setCommentContext(findByMg.getCommentContext());
        }
        if (modifyInfo.getCreatetime()==null&&findByMg.getCreatetime()!=null){
            modifyInfo.setCreatetime(findByMg.getCreatetime());
        }
        if (modifyInfo.getUserName()==null&&findByMg.getUserName()!=null){
            modifyInfo.setUserName(findByMg.getUserName());
        }
        if (modifyInfo.getOrderNum()==null&&findByMg.getOrderNum()!=null){
            modifyInfo.setOrderNum(findByMg.getOrderNum());
        }
        if (modifyInfo.getAvPrice()==null&&findByMg.getAvPrice()!=null){
            modifyInfo.setAvPrice(findByMg.getAvPrice());
        }
        if (modifyInfo.getImgList()==null&&findByMg.getImgList()!=null){
            modifyInfo.setImgList(findByMg.getImgList());
        }
        if (modifyInfo.getReplyUserId()==null&&findByMg.getReplyUserId()!=null){
            modifyInfo.setReplyUserId(findByMg.getReplyUserId());
        }
        if (modifyInfo.getReplyUserName()==null&&findByMg.getReplyUserName()!=null){
            modifyInfo.setReplyUserName(findByMg.getReplyUserName());
        }
        if (modifyInfo.getLikeNum()==null&&findByMg.getLikeNum()!=null){
            modifyInfo.setLikeNum(findByMg.getLikeNum());
        }
        if (modifyInfo.getReplynum()==null&&findByMg.getReplynum()!=null){
            modifyInfo.setReplynum(findByMg.getReplynum());
        }
        if (modifyInfo.getReplynum()==-1){
            if (findByMg.getReplynum()!=null){
                modifyInfo.setReplynum(findByMg.getReplynum()+1);
            }else {
                modifyInfo.setReplynum(1);
            }
        }
        if (modifyInfo.getParentId()==null&&findByMg.getParentId()!=null){
            modifyInfo.setParentId(findByMg.getParentId());
        }
        if (modifyInfo.getStatus()==null&&findByMg.getStatus()!=null){
            modifyInfo.setStatus(findByMg.getStatus());
        }
        return modifyInfo;
    }

}
